package com.community.body.service.impl;

import java.util.Date;
import java.util.List;

import com.community.body.domain.ZyCommunity;
import com.community.body.dto.OwnerCommunityDTO;
import com.community.body.dto.ZyCommunityDTO;
import com.community.body.mapper.ZyCommunityMapper;
import com.community.body.service.IZyCommunityService;
import com.community.body.vo.EchartsVo;
import com.community.common.core.domain.entity.SysUser;
import com.community.common.exception.ServiceException;
import com.community.common.utils.DateUtils;
import com.community.common.utils.SecurityUtils;
import com.community.common.utils.StringUtils;
import com.community.common.utils.bean.BeanValidators;
import com.community.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.Validator;

import static com.community.common.utils.sign.Md5Utils.log;


/**
 * buildingsService业务层处理
 * 
 * @author community
 * @date 2024-04-09
 */
@Service
public class ZyCommunityServiceImpl implements IZyCommunityService
{
    @Resource
    private ZyCommunityMapper zyCommunityMapper;

    @Resource
    private ISysConfigService configService;

    @Autowired
    protected Validator validator;

    /**
     * 查询buildings
     * 
     * @param communityId buildings主键
     * @return buildings
     */
    @Override
    public ZyCommunity selectZyCommunityByCommunityId(Long communityId)
    {
        return zyCommunityMapper.selectZyCommunityByCommunityId(communityId);
    }

    /**
     * 查询buildings列表
     * 
     * @param zyCommunity buildings
     * @return buildings
     */
    @Override
    public List<ZyCommunity> selectZyCommunityList(ZyCommunityDTO zyCommunity)
    {
        return zyCommunityMapper.selectZyCommunityList(zyCommunity);
    }

    /**
     * 新增buildings
     * 
     * @param zyCommunity buildings
     * @return 结果
     */
    @Override
    public int insertZyCommunity(ZyCommunity zyCommunity)
    {

        zyCommunity.setCommunityCode("COMMUNITY_"+System.currentTimeMillis());
        zyCommunity.setCreateTime(DateUtils.getNowDate());
        return zyCommunityMapper.insertZyCommunity(zyCommunity);
    }

    /**
     * 修改buildings
     * 
     * @param zyCommunity buildings
     * @return 结果
     */
    @Override
    public int updateZyCommunity(ZyCommunity zyCommunity)
    {
        zyCommunity.setUpdateTime(DateUtils.getNowDate());
        return zyCommunityMapper.updateZyCommunity(zyCommunity);
    }

    /**
     * 批量删除buildings
     * 
     * @param communityIds 需要删除的buildings主键
     * @return 结果
     */
    @Override
    public int deleteZyCommunityByCommunityIds(Long[] communityIds)
    {
        return zyCommunityMapper.deleteZyCommunityByCommunityIds(communityIds);
    }

    /**
     * 删除buildings信息
     * 
     * @param communityId buildings主键
     * @return 结果
     */
    @Override
    public int deleteZyCommunityByCommunityId(Long communityId)
    {
        return zyCommunityMapper.deleteZyCommunityByCommunityId(communityId);
    }

    @Override
    public String importCommunity(List<ZyCommunity> communityList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(communityList) || communityList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ZyCommunity community : communityList)
        {
            try
            {
                // 验证是否存在这个用户
                ZyCommunity u = zyCommunityMapper.selectZyCommunityByCommunityName(community.getCommunityName());
                if (StringUtils.isNull(u) || !u.getCommunityTownCode().equals(community.getCommunityTownCode()))
                {
                    BeanValidators.validateWithException(validator, community);
                    community.setCreateTime(new Date());
                    zyCommunityMapper.insertZyCommunity(community);
                    successNum++;
                    successMsg.append("<br/>").append(successNum).append("、账号 ").append(community.getCommunityName()).append(" 导入成功");
                }
                else if (updateSupport)
                {
                    BeanValidators.validateWithException(validator, community);
                    community.setCommunityId(u.getCommunityId());
                    zyCommunityMapper.updateZyCommunity(community);
                    successNum++;
                    successMsg.append("<br/>").append(successNum).append("、账号 ").append(community.getCommunityName()).append(" 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>").append(failureNum).append("、账号 ").append(community.getCommunityName()).append(" 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + community.getCommunityName() + " 导入失败：";
                failureMsg.append(msg).append(e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<ZyCommunity> selectZyCommunityExport(Long[] arr) {
        return zyCommunityMapper.selectZyCommunityExport(arr);
    }

    @Override
    public List<ZyCommunityDTO> selectZyCommunityList3(ZyCommunityDTO zyCommunityDTO) {
        return zyCommunityMapper.selectZyCommunityList3(zyCommunityDTO);
    }

    @Override
    public List<EchartsVo> getMap(ZyCommunity zyCommunity) {
        return zyCommunityMapper.getMap(zyCommunity);
    }

    @Override
    public List<EchartsVo> getPersonList(SysUser user) {
        return zyCommunityMapper.getPersonList(user);
    }

    @Override
    public List<OwnerCommunityDTO> selectZyCommunityList5(OwnerCommunityDTO ownerCommunityDTO) {
        return zyCommunityMapper.selectZyCommunityList5(ownerCommunityDTO);
    }
}
